﻿using System;
using System.Web.Security;
using System.Web.Services;
using MonkeyShop.Admin.Biz;
using MonkeyShop.Web.Administration.Common;

namespace MonkeyShop.Web.Administration.Employees
{
    public partial class List : System.Web.UI.Page
    {
        #region Properties

        private static AdminUserBiz _adminUserBiz;
        private static AdminUserBiz AdminUserManager
        {
            get { return _adminUserBiz ?? (_adminUserBiz = new AdminUserBiz()); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (AdminCommon.CurrrentEmployee == null)
            {
                FormsAuthentication.RedirectToLoginPage();
                return;
            }

            if (!AdminCommon.CurrrentEmployee.IsAdmin)
            {
                Response.Redirect("~/Administration/NoPermission.aspx");
            }
        }

        #region Web methods

        [WebMethod(EnableSession = true)]
        public static object EmployeeList(int jtStartIndex, int jtPageSize, string jtSorting, string name)
        {
            int totalRecords;
            var results = AdminUserManager.GetEmployees(jtStartIndex, jtPageSize, jtSorting, name, out totalRecords);
            return new { Result = "OK", Records = results, TotalRecordCount = totalRecords };
        }

        [WebMethod(EnableSession = true)]
        public static object DeleteEmployee(int Id)
        {
            if (AdminCommon.CurrrentEmployee == null || !AdminCommon.CurrrentEmployee.IsAdmin || AdminCommon.CurrrentEmployee.Id == Id)
                return new { Result = "ERROR", Message = "Error" };

            AdminUserManager.DeleteEmployee(Id);
            return new { Result = "OK" };
        }

        #endregion
    }
}